package com.qy.controller.manage;

import com.github.pagehelper.PageHelper;
import com.qy.base.core.PageBean;
import com.qy.base.core.Result;
import com.qy.base.core.ResultGenerator;
import com.qy.base.utils.UploadFile;
import com.qy.model.CopyWriting;
import com.qy.service.CopyWritingService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by zaq on 2018/07/10.
 */
@RestController("Manage-CopyWritingController")
@RequestMapping("manage/copy/writing")
public class CopyWritingController {
    @Resource
    private CopyWritingService copyWritingService;

    @PostMapping("/add")
    public Result add(@RequestBody CopyWriting copyWriting) {
        copyWritingService.save(copyWriting);
        return ResultGenerator.successResult();
    }

    @PostMapping("/delete")
    public Result delete(@RequestBody Integer id) {
        copyWritingService.deleteById(id);
        return ResultGenerator.successResult();
    }

    //编辑
    @PostMapping("/update")
    public Result update(@RequestBody CopyWriting copyWriting) {
        CopyWriting copyWriting1 = copyWritingService.findById(copyWriting.getId());
        if (copyWriting.getApply_process() != null && !copyWriting.getApply_process().isEmpty()) {
            copyWriting1.setApply_process(copyWriting.getApply_process());
        }
        if (copyWriting.getExperience_share() != null && !copyWriting.getExperience_share().isEmpty()) {
            copyWriting1.setExperience_share(copyWriting.getExperience_share());
        }
        if (copyWriting.getManage_unit() != null && !copyWriting.getManage_unit().isEmpty()) {
            copyWriting1.setManage_unit(copyWriting.getManage_unit());
        }
        if (copyWriting.getThird_promote() != null && !copyWriting.getThird_promote().isEmpty()) {
            copyWriting1.setThird_promote(copyWriting.getThird_promote());
        }
        if (copyWriting.getUse_information() != null && !copyWriting.getUse_information().isEmpty()) {
            copyWriting1.setUse_information(UploadFile.uploadBase64(copyWriting.getUse_information()));
        }
        if (copyWriting.getPolicy_explain() != null && !copyWriting.getPolicy_explain().isEmpty()){
            copyWriting1.setPolicy_explain(UploadFile.uploadBase64(copyWriting.getPolicy_explain()));
        }
        if (copyWriting.getReward_explain() != null && !copyWriting.getReward_explain().isEmpty()){
            copyWriting1.setReward_explain(UploadFile.uploadBase64(copyWriting.getReward_explain()));
        }
        if (copyWriting.getMember_explain() != null && !copyWriting.getMember_explain().isEmpty()){
            copyWriting1.setMember_explain(UploadFile.uploadBase64(copyWriting.getMember_explain()));
        }

        copyWritingService.update(copyWriting1);
        return ResultGenerator.successResult();
    }

    @GetMapping("/detail")
    public Result detail(@RequestParam Integer id) {
        CopyWriting copyWriting = copyWritingService.findById(id);
        return ResultGenerator.successResult(copyWriting);
    }

    //文案管理
    @GetMapping("/list")
    public Result list() {
        List<CopyWriting> list = copyWritingService.findAll();
        return ResultGenerator.successResult(list);
    }

}
